<template>
  <div>
    <el-table
      border
      :data="data"
      highlight-current-row
      @current-change="currentChange"
    >
      <el-table-column
        v-for="(item, index) in config"
        :key="index"
        :label="item.label"
      >
        <template slot-scope="scope">
          <input
            type="radio"
            v-if="item.type === 'radio'"
            name="aa"
            :checked="scope.row === currentRow"
          />
          <el-select
            v-model="scope.row[item.prop]"
            :remote="item.remote"
            filterable
            :remote-method="item.remoteMethod"
            v-else-if="item.type === 'select'"
          >
            <el-option
              v-for="(op, ind) in item.options"
              :key="ind"
              :label="op.label"
              :value="op.value"
            ></el-option>
          </el-select>
          <el-input
            v-model="scope.row[item.prop]"
            size="mini"
            v-else-if="item.type === 'input'"
          ></el-input>
          <div v-else>{{ scope.row[item.prop] }}</div>
        </template>
      </el-table-column>
      <el-table-column v-if="canAdd">
        <template slot-scope="scope">
          <div @click="minus(scope.$index)">-</div>
        </template>
        <template slot="header">
          <div class="add" @click="addRow">+</div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  props: {
    canAdd: {
      type: Boolean,
      default: () => false,
    },
    data: {
      type: Array,
      default: () => [],
    },
    config: {
      type: Array,
      default: () => [],
    },
    currentRow: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {};
  },
  methods: {
    addRow() {
      this.data.push(
        this.config.reduce((pre, cur) => {
          pre[cur.prop] = "";
          return pre;
        }, {})
      );
      this.$emit("update:data", this.data);
    },
    minus(index) {
      this.data.splice(index, 1);
      this.$emit("update:data", this.data);
    },
    currentChange(val) {
      this.$emit("update:currentRow", val);
    },
  },
};
</script>

<style scoped>
.add {
  cursor: pointer;
}
</style>
